package com.leo.blog.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dao.ResourcesDao;
import com.leo.blog.dto.ResourcesExecution;
import com.leo.blog.entity.Resources;
import com.leo.blog.enums.ResourcesStateEnum;
import com.leo.blog.service.ResourcesService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.PageCalculator;

@Service
public class ResourcesServiceImpl implements ResourcesService{
	@Autowired
	private ResourcesDao resourcesDao;

	@Override
	@Transactional
	@SystemLog(description = ConstantUtil.RESOURCE_ADD,userType=ConstantUtil.USERTYPE_ADMIN)
	public ResourcesExecution addResources(String name, Resources resources) {
		//判断对象是否为空
		if(resources != null) {
			//初始化创建时间
			resources.setCreateTime(new Date());
			
			int effectedNum = resourcesDao.insertResources(resources);
			if(effectedNum > 0) {
				return new ResourcesExecution(ResourcesStateEnum.SUCCESS);
			}else {
				return new ResourcesExecution(ResourcesStateEnum.OFFLINE);//-1
			}
		}else {
			return new ResourcesExecution(ResourcesStateEnum.NULL_RESOURCES);
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.RESOURCE_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN)
	public ResourcesExecution updateResources(String name, Resources resources) {
		//判断
		if(resources != null) {
			int effectedNum = resourcesDao.updateResources(resources);
			if(effectedNum > 0) {
				return new ResourcesExecution(ResourcesStateEnum.SUCCESS);
			}else {
				return new ResourcesExecution(ResourcesStateEnum.OFFLINE);//-1
			}
		}else {
			return new ResourcesExecution(ResourcesStateEnum.NULL_RESOURCES);
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.RESOURCE_DELETE,userType=ConstantUtil.USERTYPE_ADMIN)
	public ResourcesExecution deleteResources(String name, int resourcesId) {
		//判断
		if(resourcesId > 0) {
			int effectedNum = resourcesDao.deleteResources(resourcesId);
			if(effectedNum > 0) {
				return new ResourcesExecution(ResourcesStateEnum.SUCCESS);
			}else {
				return new ResourcesExecution(ResourcesStateEnum.OFFLINE);//-1
			}
		}else {
			return new ResourcesExecution(ResourcesStateEnum.NULL_RESOURCES);
		}
	}

	@Override
	public ResourcesExecution getResourcesById(int resourcesId) {
		//判断
		if(resourcesId > 0) {
			Resources resources = resourcesDao.queryResourcesById(resourcesId);
			if(resources != null) {
				return new ResourcesExecution(ResourcesStateEnum.SUCCESS, resources);
			}else {
				return new ResourcesExecution(ResourcesStateEnum.OFFLINE);//-1
			}
		}else {
			return new ResourcesExecution(ResourcesStateEnum.NULL_RESOURCES);
		}
	}

	@Override
	public ResourcesExecution getResourcesList(Resources resourcesCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据查询条件,调用dao层返回相关的资源信息列表
		List<Resources> resourcesList = resourcesDao.queryResourcesList(resourcesCondition, rowIndex, pageSize);
		//根据相同的查询条件,返回资源总数
		int count = resourcesDao.queryResourcesCount(resourcesCondition);
		ResourcesExecution re = new ResourcesExecution();
		if(resourcesList != null) {
			re.setResourcesList(resourcesList);
			re.setCount(count);
		}else {
			re.setState(ResourcesStateEnum.INNER_ERROR.getState());
		}
		return re;
	}

	@Override
	public ResourcesExecution getResourcesListByStatus() {
		List<?> resourcesList2 = resourcesDao.queryResourcesListByStatus();
		ResourcesExecution re = new ResourcesExecution();
		if (resourcesList2.size() > -1) {
			re.setResourcesList2(resourcesList2);
			re.setState(ResourcesStateEnum.SUCCESS.getState());
		}else {
			re.setState(ResourcesStateEnum.OFFLINE.getState());
		}
		
		return re;
	}

}
